/**
 * copyright © 2006-2007 geolabs
 * author : Gérald Fenoy gerald [ at ] geolabs [ dot ] fr
 *
 */
/**
 * Window Class 
 */
System.require("Desktop");
System.require("Position");
System.require("Effect");
System.require("Styler");

Window=Class.create();
Window.define({

  _init: function(){
      if(arguments[0]){
	/**
	 * The window' name
	 */
	this.name = arguments[0].name;
	
	/**
	 * Does it must be displayed or moved over "desktop"
	 */
	this.dragable = false;
	this.resizeable = false;
	this.active = true;
	
	if(!arguments[0].display){
	  /**
	   * Display
	   */
	  var root = System.document.body;
	  
	  /**
	   * Global container
	   * fixing the size of the window
	   */
	  var elbg = System.document.createElement('iframe');
	  var el = System.document.createElement('div');
	  el.setAttribute('style', 'width:'+arguments[0].width+'px;');
	  elbg.setAttribute('style', 'width:'+arguments[0].width+'px;height:'+(arguments[0].height+10)+"px");
	  el.setAttribute('class', 'octowin');
	  if(Styler._unbound){
	    el.className='octowin';
	    el.style.width=arguments[0].width+"px";
	    el.style.height=(arguments[0].height+10)+"px";
	    elbg.style.width=arguments[0].width+"px";
	    elbg.style.height=(arguments[0].height+10)+"px";
	  }
	  el.setAttribute('id',arguments[0].name);
	  elbg.setAttribute('id',arguments[0].name+'_bg');

	  var bi = System.document.createElement('div');
	  bi.setAttribute('class', 'bi');
	  bi.className='bi';
	  
	  var bt = System.document.createElement('div');
	  bt.setAttribute('class', 'bt');
	  bt.className='bt';
	  var ibt = System.document.createElement('div');
	  bt.appendChild(ibt);
	  
	  bi.appendChild(bt);
	  
	  var octowinContent=System.document.createElement('div');
	  octowinContent.setAttribute('class', 'octoWinContent');
	  octowinContent.className='octoWinContent';
	  
	  /**
	   * TITLEBAR
	   */
	  var el1 = System.document.createElement('div');
	  el1.setAttribute('class', 'title');
	  el1.className='title';
	  el1.setAttribute('id', arguments[0].name+'Title');
	  
	  var buttonBar = System.document.createElement('div');
	  buttonBar.setAttribute('class', 'button');
	  buttonBar.className='button';
	  buttonBar.setAttribute('id', arguments[0].name+'Buttons');
	  
	  imgH=System.document.createElement('img');
	  imgH.setAttribute('class', 'first');
	  imgH.className='first';
	  imgH.setAttribute('src', './images/minimize.png');
	  imgH.setAttribute('id', arguments[0].name+'minimize');
	  imgH.onclick=this.displayContent.bind(this);
	  buttonBar.appendChild(imgH);
	  
	  imgH=System.document.createElement('img');
	  imgH.setAttribute('class', 'second');
	  imgH.className='second';
	  imgH.setAttribute('id', arguments[0].name+'maximize');
	  imgH.setAttribute('src', './images/maximize.png');
	  imgH.onclick=this.setFullScreen.bind(this);
	  buttonBar.appendChild(imgH);
	  
	  imgH=System.document.createElement('img');
	  imgH.setAttribute('class', 'last');
	  imgH.className='last';
	  imgH.setAttribute('src', './images/close.png');
	  imgH.setAttribute('id', arguments[0].name+'close');
	  imgH.onclick=this.hide.bind(this);
	  buttonBar.appendChild(imgH);
	  
	  titleField=System.document.createElement("div");
	  title=System.document.createTextNode(arguments[0].title);
	  titleField.appendChild(title);
	  el1.appendChild(titleField);
	  el1.appendChild(buttonBar);
	  octowinContent.appendChild(el1);
	  
	  /**
	   * Optional menu
	   */
	  this.hasMenu=false;
	  if(arguments[0].hasMenu){
	    imgH=System.document.createElement('div');
	    imgH.setAttribute('id', arguments[0].name+'Menu');
	    imgH.setAttribute('class', 'wmenubar');
	    imgH.style.clear='none';
	    imgH.className='wmenubar';
	    this.hasMenu=true;
	    octowinContent.appendChild(imgH);
	  }

	  /**
	   * CONTENT
	   */
	  var wcontent = System.document.createElement('div');
	  wcontent.setAttribute('class', 'wcontent');
	  wcontent.className='wcontent';
	  wcontent.setAttribute('id', arguments[0].name+'C');
	  
	  octowinContent.appendChild(wcontent);
	  
	  /**
	   * STATUSBAR
	   */
	  content1 = System.document.createElement('div');
	  content1.setAttribute('class', 'wstatus');
	  content1.className='wstatus';
	  content1.setAttribute('id', this.name+'Status');
	  
	  content2 = System.document.createElement('div');
	  content2.setAttribute('class', 'statustext');
	  content2.className='statustext';
	  content2.setAttribute('id', arguments[0].name+'StatusText');
	  content2.innerHTML='en attente';
	  
	  content3 = System.document.createElement('div');
	  content3.setAttribute('class', 'resizer');
	  content3.setAttribute('id', arguments[0].name+'Resizer');
	  
	  content1.appendChild(content2);
	  content1.appendChild(content3);
	  
	  octowinContent.appendChild(content1);
	  
	  //if(!Styler._unbound){
	  var last=System.document.createElement((Styler._unbound?"div":"br"));
	  if(!Styler._unbound)
	    last.setAttribute("style","clear: both;height:0px;");
	  else{
	    last.style.height="11px";
	  }
	  last.style.clear=/*(Styler._unbound?"none":*/"both"/*)*/;

	  //if(Styler._unbound) last.style.display='none';
	    
	  octowinContent.appendChild(last);
	    //}
	  bi.appendChild(octowinContent);
	  
	  var bb = System.document.createElement('div');
	  bb.setAttribute('class', 'bb');
	  bb.className='bb';
	  var ibb = System.document.createElement('div');
	  bb.appendChild(ibb);
	  
	  bi.appendChild(bb);
	  
	  el.appendChild(bi);
	  
	  root.appendChild(elbg);
	  root.appendChild(el);
	}
	/* End Display */
	
	this.initializeLink();
	
	this.titleBar.className='title';
	
	this.titleBar.onclick=this.setImg.bind(this);
	
	this.winObj.style.height=(arguments[0].height+62+(Styler._unbound?6:0)+(this.hasMenu?Position.sizeIntoElement(this.menubar).y:0))+'px';
	this.winObjBg.style.height=(arguments[0].height+62+(Styler._unbound?6:0)+(this.hasMenu?Position.sizeIntoElement(this.menubar).y:0))+'px';

	//alert(Position.sizeIntoElement(this.menubar).y);
	
	this.wcontent.className='wcontent';
	this.wcontent.style.height=(arguments[0].height/*-(Styler._unbound?10:0)*/)+'px';
	
	this.statusBar.style.width=(arguments[0].width-/*(Styler._unbound?23:*/15/*)*/)+'px';
	//this.statusBar.style.width='96%';
	
	this.resizer.style.position='absolute';
	if(Styler._unbound){
	  this.resizer.className='resizer';
	  this.resizer.style.right='0px';
	}
	else{
	  this.resizer.style.right='1px';
	}
	this.resizer.onclick=this.setResizerImg.bind(this);
	
	/**
	 * Positionning
	 */
	this.position=new Position(arguments[0].x,arguments[0].y);
	this.initialPosition=new Position(0,0);
	this.oldPosition=new Position(arguments[0].x,arguments[0].y);
	
	/**
	 * Size
	 */
	this.size=new Position(arguments[0].width,arguments[0].height);
	this.initialSize=new Position(0,0);
	this.oldSize=new Position(arguments[0].width,arguments[0].height);
      }
    },
	
  initializeLink: function(){
	
      this.winObj=System.document.getElementById(this.name);
      this.winObjBg=document.getElementById(this.name+'_bg');
      
      try{ this.titleBar=System.document.getElementById(this.name+'Title'); }
      catch(e){ this.hasTitle=false; }

      try{ this.menubar=System.document.getElementById(this.name+'Menu'); }
      catch(e){ this.hasMenu=false; }

      try{ this.wcontent=System.document.getElementById(this.name+'C'); }
      catch(e){ this.hasWcontent=false; }
      
      try{this.statusBar=System.document.getElementById(this.name+"Status");}
      catch(e){ this.hasStatusBar=false; }
      
      try{this.statusText=System.document.getElementById(this.name+"StatusText");}
      catch(e){ this.hasStatusText=false; }
      
      try{ 
	this.resizer = System.document.getElementById(this.name+"Resizer"); 
	this.resizer.onclick=this.setResizerImg.bind(this);
      }
      catch(e){ this.hasResizer=false; }
      
    },


  id: 0,
  desktop: null,

  displayNextStep: function(){

      if(!this.nbStep && this.nbStep!=-1.5 && this.nbStep!=0.0)
	this.nbStep=-1.5;
      if(this.nbStep<9.0)
	this.nbStep+=1.0;

      if(this.nbStep>0)
	Styler.setOpacity(this.winObj,this.nbStep);
      else
	Styler.setOpacity(this.winObj,0.0);
      Styler.setOpacity(this.winObjBg,0.1);

    },

  displayFinalStep: function(){ Styler.setOpacity(this.winObj,10.0);Styler.setOpacity(this.winObjBg,7.0); },

  setId: function(id){ this.id=id;},
      
  setDesktop: function(d){ this.desktop=d; },
      
  hide: function(){
      if(this.winObj){
	this.desktop.removeWin(this);
	try{
	  System.document.body.removeChild(this.winObj);
	  System.document.body.removeChild(this.winObjBg);
	  delete(this);
	}
	catch(e){;}
	/**
	 * More intuitive method to remove an object, but it imply an object 
	 * error in the ie browser.
	 * delete(this);
	 */
      }
	/*	if(this.winObj.style.display=='none'){
	  this.winObj.style.display='block';
	  Effect.slide({start:2,end:10,iwidth:this.size.x,height:this.size.y,id:this.name});

	  this.statusBar.style.display='block';
	  this.resizer.style.display='block';
	}
	else{
	  Effect.slide({start:2,end:10,iwidth:this.size.x,height:this.size.y,id:this.name});
	  setTimeout("System.document.getElementById('"+this.winObj.id+"').style.display='none';System.document.getElementById('"+this.statusBar.id+"').style.display='none';System.document.getElementById('"+this.resizer.id+"').style.display='none';",1000);
	  }*/
    },
      
  unsetFocus: function(){
      /* Here we must use a list of windows to reset zIndex value */
      if(this.winObj){
	this.winObj.style.zIndex=40;
	this.winObjBg.style.zIndex=35;
      }
    },
    
  debug :function(){
      return "Name : "+this.name+"\n dragable : "+this.dragable+"\n resizeable : "+this.resizeable+"\n this.position : ["+this.position.x+","+this.position.y+"]\n this.initialPosition : ["+this.initialPosition.x+","+this.initialPosition.y+"]\n this.oldPosition : ["+this.oldPosition.x+","+this.oldPosition.y+"]"+"\n this.size : ["+this.size.x+","+this.size.y+"]\n this.initialSize : ["+this.initialSize.x+","+this.initialSize.y+"]\n this.oldSize : ["+this.oldSize.x+","+this.oldSize.y+"]";
    },
    
  getPosition: function(){ 
      return new Array(this.position.x,this.position.y);
    },

 
  getSize: function(){return new Array(this.size.x,this.size.y);},

  getOldPosition: function(obj){
      
      if(this.position.x!=0){
	obj.style.left=this.position.x + 'px';
	obj.style.top=this.position.y + 'px';
	System.document.getElementById(obj.id+'_bg').style.position='absolute';
	System.document.getElementById(obj.id+'_bg').style.left=this.position.x + 'px';
	System.document.getElementById(obj.id+'_bg').style.top=this.position.y + 'px';

      }

      if(this.active>0)
	this.wcontent.style.display="block";
      else
	this.wcontent.style.display="none";
    },

  displayContent: function(){

      if(this.wcontent.style.display=='none'){
	this.winObjBg.style.display='block';
	this.wcontent.style.display='block';
	//this.statusBar.style.display='block';
	this.resizer.style.display='block';
	this.winObj.style.width=this.size.x+"px";
	this.winObj.style.height=(this.size.y+64+(Styler._unbound?10:0)+(this.hasMenu?this.save_menubar_height.y:0))+"px";
	this.winObjBg.style.width=this.size.x+"px";
	this.winObjBg.style.height=(this.size.y+64+(Styler._unbound?10:0)+(this.hasMenu?this.save_menubar_height.y:0))+"px";

	if(this.hasMenu)
	  this.menubar.style.display="block";
	  //this.menubar.style.height=this.save_menubar_height+'px';

	/*Effect.slide({start:1,end:10,iwidth:this.size.x,height:(this.size.y),id:this.winObj.id});
	  Effect.slide({start:1,end:10,iwidth:this.size.x,height:(this.size.y+64),id:this.wcontent.id});
	  setTimeout("System.document.getElementById('"+this.statusBar.id+"').style.display='block';System.document.getElementById('"+this.resizer.id+"').style.display='block';",250);*/

	this.active=true;
      }
      else {
	if(this.hasMenu){
	  this.save_menubar_height=Position.sizeIntoElement(this.menubar);
	  this.menubar.style.display="none";
	}
	this.wcontent.style.display='none';
	//this.statusBar.style.display='none';
	this.resizer.style.display='none';
	this.winObj.style.height=(56+(Styler._unbound?10:0))+"px";
	this.winObjBg.style.height=(56+(Styler._unbound?10:0))+"px";
	/*Effect.slide({start:10,end:1,iwidth:this.size.x,height:(this.size.y),id:this.winObj.id});
	  Effect.slide({start:10,end:1,iwidth:this.size.x,height:(this.size.y+64),id:this.wcontent.id});
	  setTimeout("System.document.getElementById('"+this.wcontent.id+"').style.display='none';System.document.getElementById('"+this.statusBar.id+"').style.display='none';System.document.getElementById('"+this.resizer.id+"').style.display='none';",1500);*/

	/*if(Styler._unbound)
	  this.winObj.style.height="15px";*/

	this.active=false;
      }
    },


  setFullScreen: function(){
      var size=new Position(this.oldSize.x,this.oldSize.y);
      var position=Array(this.position.x,this.position.y);
      var image='./images/maximize.png';
      //this.resizer.onclick=new Function("alert('impossible');");
      var maximize=false;

      if(System.document.getElementById(this.name+'maximize').src.indexOf("unmaximize") < 0){
	maximize=true;
	var size=Position.fullSize();
	position=Array(0,0);
	this.resizer.onmousedown=null;
      
	size.x-=40;
	size.y-=(40+64);
	//alert(size.x+" "+size.y);
	image='./images/unmaximize.png';
	this.resizer.style.display='none';
	this.titleBar.onclick=new Function();

      }
      else{
	this.resizer.style.display='block';
	this.resizer.onclick=this.setResizerImg.bind(this);
	this.titleBar.onclick=this.setImg.bind(this);
      }

      this.winObj.style.left=position[0]+'px';
      this.winObj.style.top=position[1]+'px';
      this.winObjBg.style.left=position[0]+'px';
      this.winObjBg.style.top=position[1]+'px';

      this.winObj.style.width = size.x + 'px';
      this.winObj.style.height = (size.y+64+(Styler._unbound?10:0)) + 'px';
      this.winObjBg.style.width = size.x + 'px';
      this.winObjBg.style.height = (size.y+64+(Styler._unbound?10:0)) + 'px';
      this.statusBar.style.width = (size.x-/*(Styler._unbound?23:*/15/*)*/) + 'px';
      
      this.wcontent.style.height=(size.y)+'px';

      System.document.getElementById(this.name+'maximize').src=image;
      this.size=size;
    },

      /**
       * MOVE
       */
  noScroll: function(){
      this.wcontent.style.overflow="hidden";
    },

  setImg: function(){
      this.desktop.setActiveWindow(this);
      this.desktop.displayBg();

      this.wcontent.style.overflow="auto";

      this.titleBar.onmousedown=this.selectImg.bind(this);
      System.document.onmouseup=this.unsetImg.bind(this);
      this.winObj.onmouseup=this.unsetImg.bind(this);

    },

  unsetImg: function(){
      this.dragable=false;

      
      //this.wcontent.style.overflow="hidden";
      this.desktop.hideBg();

      Styler.setOpacity(this.winObj,9.5);

      this.desktop.updateWindowsZIndex();
      
      this.titleBar.onmousedown=new Function();
      System.document.onmouseup=new Function();
      this.winObj.onmouseup=new Function();

      this.titleBar.onmousemove=new Function();
      this.winObj.onmousemove=new Function();
      System.document.onmousemove=new Function();

    },

  selectImg: function(){

      this.dragable=true;
      var pos=Position.fromEvent(arguments[0]);

      this.initialPosition.x = pos.x;
      this.initialPosition.y = pos.y;

      this.initialPosition.x -= this.position.x;
      this.initialPosition.y -= this.position.y;

      Styler.setOpacity(this.winObj,4.5);

      this.titleBar.onmousedown=new Function();

      this.titleBar.onmousemove=this.moveImg.bind(this);
      this.winObj.onmousemove=this.moveImg.bind(this);
      System.document.onmousemove=this.moveImg.bind(this);

    },

  moveImg: function(){
      if(this.dragable)
	{
	  var pos=Position.fromEvent(arguments[0]);

	  if(((pos.x - this.initialPosition.x)>0 && (pos.y - this.initialPosition.y)>0)
	     &&
	     (((pos.x - this.initialPosition.x)+(this.size.x) < this.desktop.size.x) && 
	      ((pos.y - this.initialPosition.y)+(this.size.y+64+(Styler._unbound?10:0)) < this.desktop.size.y))){

	    this.position.x = pos.x - this.initialPosition.x;
	    this.position.y = pos.y - this.initialPosition.y;

	    this.winObj.style.left = this.position.x + 'px';
	    this.winObj.style.top = this.position.y + 'px';
	    this.winObjBg.style.left = this.position.x + 'px';
	    this.winObjBg.style.top = this.position.y + 'px';

	  }
	}

    },
  /**
   * END MOVE
   */

  /**
   * SIZE
   */
  setResizerImg: function(){
      this.desktop.displayBg();

      System.document.onmousedown=this.selectResizerImg.bind(this);
      this.resizer.onmousedown=this.selectResizerImg.bind(this);
      System.document.onmouseup=this.unsetResizerImg.bind(this);
      this.resizer.onmouseup=this.unsetResizerImg.bind(this);

    },

  unsetResizerImg: function(){
      this.desktop.hideBg();

      this.resizeable=false;
      Styler.setOpacity(this.winObj,9.5);
      this.desktop.updateWindowsZIndex();
      System.document.onmousedown=new Function();
      this.resizer.onmousedown=this.selectResizerImg.bind(this);
      System.document.onmouseup=new Function();
      this.resizer.onmouseup=new Function();
      this.winObj.onmousemove=new Function();
      System.document.onmousemove=new Function();

    },

  selectResizerImg: function(e){

      this.desktop.setActiveWindow(this);
      var pos=Position.fromEvent(e);
      this.initialSize.x=pos.x;
      this.initialSize.y=pos.y;
      this.initialSize.x -= this.oldSize.x;
      this.initialSize.y -= this.oldSize.y;
      this.resizeable=true;
      Styler.setOpacity(this.winObj,6.5);
      System.document.onmousemove=this.resizeImg.bind(this);
      this.winObj.onmousemove=this.resizeImg.bind(this);

    },


  resizeImg: function(e){
      if(this.resizeable)
	{

	  var tmp=Position.fromEvent(e);
	  var pos=tmp;
	  this.size.x = pos.x - this.initialSize.x;
	  this.size.y = pos.y - this.initialSize.y;

	  if(this.size.x>50 && this.size.y>50){

	    this.winObj.style.width = this.size.x + 'px';
	    this.winObj.style.height = (this.size.y+64+(Styler._unbound?10:0)+(this.hasMenu?Position.sizeIntoElement(this.menubar).y:0)) + 'px';
	    this.winObjBg.style.width = this.size.x + 'px';
	    this.winObjBg.style.height = (this.size.y+64+(Styler._unbound?10:0)+(this.hasMenu?Position.sizeIntoElement(this.menubar).y:0)) + 'px';

	    this.statusBar.style.width = (this.size.x-/*(Styler._unbound?23:*/15/*)*/) + 'px';
	    this.wcontent.style.height = (this.size.y/*-(Styler._unbound?20:0)*/) + 'px';
	    
	    this.oldSize.x=this.size.x;
	    this.oldSize.y=this.size.y;

	  }

	}

    }

  });


WindowIframe=Window.extend();
WindowIframe.define({
  setIframe: function(){
      var tmp=this.getIframeSize();
      this.wcontent.innerHTML="<iframe style='height: "+tmp.height+"px;width: "+tmp.width+"px;' src='"+arguments[0]+"' id='"+this.name+"Frame'></iframe>";
      this.frame=System.document.getElementById(this.name+"Frame");
    },
  getIframeSize: function(){
      return {width: this.size.x-(30+(Styler._unbound?5:0)), height: this.size.y-(5+(Styler._unbound?5:0))};
    },
  setIframeSize: function(){
      var tmp=this.getIframeSize();
      this.frame.style.width=tmp.width+'px';
      this.frame.style.height=tmp.height+'px';
    },
  setImg: function(){
      this.superCall('setImg',arguments);
      this.frame.onmousedown=this.selectImg.bind(this);
    },
  unsetImg: function(){
      this.superCall('unsetImg',arguments);
      this.frame.onmousedown=new Function();
    },
  setFullScreen: function(){
      this.superCall('setFullScreen',arguments);
      this.setIframeSize();
    },
  resizeImg: function(){
      this.superCall('resizeImg',arguments);
      this.setIframeSize();
    }
  });
